package com.shxc.smarttrade.index.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.index.model.MessageModel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 首页 - 消息中心 - 消息列表
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {

    private Context mContext;
    private List<MessageModel.DataBean> mList;
    private Handler mHandler;
    private boolean isOprate = false;

    public MessageAdapter(Context c, List<MessageModel.DataBean> list, Handler handler) {
        mContext = c;
        mList = list;
        mHandler = handler;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_message, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final MessageModel.DataBean model = mList.get(position);
        if (position == 0) {
            holder.first_view.setVisibility(View.VISIBLE);
        } else {
            holder.first_view.setVisibility(View.GONE);
        }
        holder.title_textview.setText(model.getMsgTitle());
        holder.content_textview.setText(model.getMsgInfo());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        holder.time_textview.setText(sdf.format(new Date(model.getCreatedAt())));

        if (model.getStatus() == 0) {
            //未读
            holder.title_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_gray));
            holder.content_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_gray));
            if (isOprate) {
                holder.point_view.setVisibility(View.GONE);
            } else {
                holder.point_view.setVisibility(View.VISIBLE);
            }
        } else {
            //已读
            holder.title_textview.setTextColor(mContext.getResources().getColor(R.color.message_gray));
            holder.content_textview.setTextColor(mContext.getResources().getColor(R.color.message_gray));
            holder.point_view.setVisibility(View.GONE);
        }

        if (model.isCheck()) {
            holder.choose_imageview.setImageResource(R.mipmap.select_round_pro);
        } else {
            holder.choose_imageview.setImageResource(R.mipmap.select_round);
        }

        if (isOprate) {
            holder.choose_imageview.setVisibility(View.VISIBLE);
        } else {
            holder.choose_imageview.setVisibility(View.GONE);
        }

        if (model.getStatus() == 0) {
            //未读
            holder.title_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_gray));
            holder.content_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_gray));
            holder.point_view.setVisibility(View.VISIBLE);
        } else {
            //已读
            holder.title_textview.setTextColor(mContext.getResources().getColor(R.color.message_gray));
            holder.content_textview.setTextColor(mContext.getResources().getColor(R.color.message_gray));
            holder.point_view.setVisibility(View.GONE);
        }

        //点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOprate) {
                    model.setCheck(!model.isCheck());
                    MessageAdapter.this.notifyDataSetChanged();
                }
                Message msg = new Message();
                msg.what = 1;
                msg.obj = model;
                mHandler.sendMessage(msg);
            }
        });
        //长按
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                model.setCheck(true);
                isOprate = true;
                notifyDataSetChanged();
                mHandler.sendEmptyMessage(2);
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void upDate(List<MessageModel.DataBean> list) {
        mList.clear();
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }
        notifyDataSetChanged();
    }

    public void setOprate(boolean oprate) {
        isOprate = oprate;
        if (!isOprate) {
            for (int i = 0; i < mList.size(); i++) {
                mList.get(i).setCheck(false);
            }
        }
        notifyDataSetChanged();
    }

    public List<MessageModel.DataBean> getChooseList() {
        List<MessageModel.DataBean> chooseList = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).isCheck()) {
                chooseList.add(mList.get(i));
            }
        }
        return chooseList;
    }

    public void checkAll(boolean isAll) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setCheck(isAll);
        }
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        protected View first_view;
        protected ImageView choose_imageview;
        protected TextView title_textview;
        protected TextView content_textview;
        protected TextView time_textview;
        protected View point_view;

        public ViewHolder(View itemView) {
            super(itemView);
            first_view = itemView.findViewById(R.id.first_view);
            choose_imageview = itemView.findViewById(R.id.choose_imageview);
            title_textview = itemView.findViewById(R.id.title_textview);
            content_textview = itemView.findViewById(R.id.content_textview);
            time_textview = itemView.findViewById(R.id.time_textview);
            point_view = itemView.findViewById(R.id.point_view);
        }
    }

}
